import { SysOptions } from '../stores/sysoptions';
import Axios from '@/api';
import { IsEmpty } from '@/utils/common';
import { LocPost } from '@/utils/LocRequestHelper';

const _sysOptStore = SysOptions();

/**
 * 获取全局系统选项
 */
export function GetGolbalOptions() {
  Axios.get(`/api/sys/sysParamValue/findSysParamFieldAndValueByCode`, {
    params: { paramCode: 'xtxxcs' }
  })
    .then((res: any) => {
      if (res && res.status && res.status.code && res.status.code === 1) {
        _sysOptStore.setGlobalOptions(res && res.data && res.data.length > 0 ? res.data : []);
      } else {
        _sysOptStore.setGlobalOptions([]);
      }
    })
    .catch((err) => {
      console.log('系统选项报错', err);
    });
}

/**
 * 获取报告单元系统选项
 */
export function GetRptGroupOptions(classifyCode: string) {
  Axios.get(`/api/sys/sysParamValue/findSysParamFieldAndValueByCode`, {
    params: { paramCode: 'bgdyxtcs', classifyCode: classifyCode }
  })
    .then((res: any) => {
      if (res && res.status && res.status.code && res.status.code === 1) {
        _sysOptStore.setRptGroupOptions(res && res.data && res.data.length > 0 ? res.data : []);
      } else {
        _sysOptStore.setRptGroupOptions([]);
      }
    })
    .catch((err) => {
      console.log('报告单元选项报错', err);
    });
}

/**
 * 获取报告单元系统选项
 */
export function GetRptGroupOptionsAsync(classifyCode: string) {
  return new Promise((resolve, reject) => {
    Axios.get(`/api/sys/sysParamValue/findSysParamFieldAndValueByCode`, {
      params: { paramCode: 'bgdyxtcs', classifyCode: classifyCode }
    })
      .then((res: any) => {
        if (res && res.status && res.status.code && res.status.code === 1) {
          _sysOptStore.setRptGroupOptions(res && res.data && res.data.length > 0 ? res.data : []);
        } else {
          _sysOptStore.setRptGroupOptions([]);
        }
        resolve(res);
      })
      .catch((err) => {
        console.log('报告单元选项报错', err);
        reject(err);
      });
  });
}
/**
 * 获取系统选项
 */
export function GetGolbalOptionsAsync() {
  return new Promise((resolve, reject) => {
    Axios.get(`/api/sys/sysParamValue/findSysParamFieldAndValueByCode`, {
      params: { paramCode: 'xtxxcs' }
    })
      .then((res: any) => {
        if (res && res.status && res.status.code && res.status.code === 1) {
          _sysOptStore.setGlobalOptions(res && res.data && res.data.length > 0 ? res.data : []);
        } else {
          _sysOptStore.setGlobalOptions([]);
        }
        resolve(res);
      })
      .catch((err) => {
        console.log('系统选项报错', err);
        reject(err);
      });
  });
}

/**
 * 获取仪器系统选项
 */
export function GetInstrOptions(classifyCode: string) {
  Axios.get(`/api/sys/sysParamValue/findSysParamFieldAndValueByCode`, {
    params: { paramCode: 'yqxtxx', classifyCode: classifyCode }
  })
    .then((res: any) => {
      if (res && res.status && res.status.code && res.status.code === 1) {
        _sysOptStore.setInstrOptions(res && res.data && res.data.length > 0 ? res.data : []);
      } else {
        _sysOptStore.setInstrOptions([]);
      }
    })
    .catch((err) => {
      console.log('获取仪器系统选项报错', err);
    });
}

function isString(value: unknown): value is string {
  return typeof value === 'string';
}

/**
 * 本地参数系统选项
 */
export async function GetLocalOptions(classifyCode: string) {
  try {
    let pc = LocPost((window as any).apiUrl.VUE_APP_LOCAL_PRINT_URL + '/api/LocalParams/get-local-pc-name', {});
    let localComputerName = isString(pc) ? pc : '';
    Axios.get(`/api/sys/sysParamValue/findSysParamFieldAndValueByCode`, {
      params: { paramCode: 'bdxtcs', localComputer: localComputerName, classifyCode: classifyCode }
    })
      .then((res: any) => {
        if (res && res.status && res.status.code && res.status.code === 1) {
          _sysOptStore.setLocalOptions(res && res.data && res.data.length > 0 ? res.data : []);
        } else {
          _sysOptStore.setLocalOptions([]);
        }
      })
      .catch((err) => {
        console.log('报告单元选项报错', err);
      });
  } catch (error) {}
}

/**
 * 根据条件查询单个选项参数
 * @paramCode   区分系统选项、报告单元选项、仪器选项、本地选项的参数
 * @classifyCode  业务Id  仪器则传仪器的Id    报告单元 则传报告单元Id
 * @fieldCode   具体参数编码
 */
export function GetOptionByConds(paramCode: string, classifyCode: string = '', fieldCode: string = '') {
  return new Promise((resolve, reject) => {
    Axios.get(`/api/sys/sysParamValue/findSysParamFieldAndValueByCode`, {
      params: { paramCode: 'bdxtcs', classifyCode: IsEmpty(classifyCode) ? null : classifyCode, fieldCode: IsEmpty(fieldCode) ? null : fieldCode }
    })
      .then((res: any) => {
        if (res && res.status && res.status.code && res.status.code === 1) {
          //_sysOptStore.setLocalOptions(res && res.data && res.data.length > 0 ? res.data : []);
          if (res && res.data && res.data.length > 0) {
            resolve(res.data[0] && IsEmpty(res.data[0].value) ? res.data[0].value : '');
          }
        } else {
          resolve(null);
        }
      })
      .catch((err) => {
        resolve('');
      });
  });
}

/**
 * 根据参数查询系统选项值
 * @code 系统选项编码
 */
export function getOptionValueByCodeGlobalOption(code: string) {
  const a = _sysOptStore.globalOptions.filter((p: any) => p.field === code);
  if (a && a.length > 0 && a[0] && !IsEmpty(a[0].value)) {
    return a[0].value;
  }
  return null;
}

/**
 * 根据参数查询报告单元选项值
 * @code 报告单元选项编码
 */
export function getOptionValueByCodeRptGroupOption(code: string) {
  const a = _sysOptStore.rptGroupOptions.filter((p: any) => p.field === code);
  if (a && a.length > 0 && a[0] && !IsEmpty(a[0].value)) {
    return a[0].value;
  }
  return null;
}

/**
 * 根据参数查询仪器项值
 * @code 仪器选项编码
 */
export function getOptionValueByCodeInstrOption(code: string) {
  const a = _sysOptStore.instrOptions.filter((p: any) => p.field === code);
  if (a && a.length > 0 && a[0] && !IsEmpty(a[0].value)) {
    return a[0].value;
  }
  return null;
}

/**
 * 根据参数查询本地选项值
 * @code 本地选项编码
 */
export function getOptionValueByCodeLocalOption(code: string) {
  const a = _sysOptStore.localOptions.find((p: any) => p.field === code);
  return IsEmpty(a) || IsEmpty(a.value) ? null : a.value;
}

/**
 * 根据系统选项Id获取系统选项值
 *@id 目标参数id
 * "1762428408081444866"
 */
export function GetGolbalOptionValueById(id: string) {
  let value = '';
  const a = _sysOptStore.globalOptions?.filter((e: any) => e.fieldId === id);
  if (a && a.length > 0) {
    return a[0].value;
  }
  return null;
}
